<?php
namespace app\admin\model;
use think\Model;
use think\Session;

class User extends Model
{


    /**
     * 查询数据
     * */
    public function userInfo($id){
        $res = $this->get($id);
        return $res;
    }

    /**
     * 查询用户数据
     * */
    public function serName($name){
        $user = new User();
        $where['name'] = ['like','%'.$name.'%'];
        $res = $user->where($where)->find();
        return $res;
    }

    /**
     * 创建登录登录Session
     * */
    public function createLogin($data){
        $user = [
            'id'=> $data['id'],
            'name' => $data['name'],
            'auth_list' => $data['name'],
        ];
        session('user',$data);
    }

    /**
     * 清除登录session
     * */
    public function clearLogin(){
        Session::set('user','');
    }

    /**
     * 检测是否登录
     * @param session('user')
     * @return  str 1：登录成功，0：失败
     * */
    public function isLogin(){
        $str = '';
        if(Session::has('user.id')==1){
            $str = 1;
        }
        return $str;
    }

    /**
     * 新增数据
     * */
    public function userSave($data){
        $res = $this->allowField(true)->save($data);
        return $res;
    }
    /**
     *更新数据
     * */
    public function userUpdate($data,$id){
        $res = $this->allowField(true)->save($data,['id'=>$id]);
        return $res;
    }
    /**
     * 批量更新
     * @param $data 多为数组
     * @param 格式 [['id'=>1,['name']=>'asdf'],['id'=>2,['name']=>'sdfef']]
     * @return  int 1;
     * */
    public function userSaveAll($data){
        $res = $this->saveAll($data);
        return $res;
    }
    /**
     * 更新权限节点
     * */
    public function userUpdateAuth($auth,$role_id){
        $data['auth_list'] = $auth;
        $res = $this->where('role_id',$role_id)->update($data);
        return $res;
    }

    /**
     * 删除管理员
     * */
    public function userDelete($id){

        if($id ==1){
            $res = 0;
        }else{
            $res = $this->destroy($id);
        }
        return $res;
    }

    /**
     * 查询管理员是否存在
     * */
    public function isName($name){
        $where['name'] = $name;
        $res = $this->where($where)->count();
        return $res;
    }


}